package org.fuxia.im.gui;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JButton;

import org.fuxia.im.common.ClientConst;
import org.fuxia.im.common.ClientContext;
import org.fuxia.im.common.CommonDialog;
import org.fuxia.im.core.entries.ECPacket;
import org.tony.common.ui.CommonFrame;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.TimerTask;

public class ModifyPwd extends CommonFrame {


	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JPasswordField newPwd;
	private JPasswordField rePwd;
	private JPasswordField ogrPAss;
	private JLabel tipLab;

	/**
	 * Create the frame.
	 */
	public ModifyPwd() {
		super();
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle("修改密码") ;
		setBounds(100, 100, 392, 259);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContent(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		panel.setOpaque(false);
		
		JLabel orignPassLab = new JLabel("原始密码：");
		orignPassLab.setHorizontalAlignment(SwingConstants.RIGHT);
		orignPassLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		orignPassLab.setBounds(64, 45, 70, 15);
		panel.add(orignPassLab);

		ogrPAss = new JPasswordField();
		ogrPAss.setBounds(144, 42, 135, 21);
		panel.add(ogrPAss);

		
		JLabel passwordLab = new JLabel("新密码：");
		passwordLab.setHorizontalAlignment(SwingConstants.RIGHT);
		passwordLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		passwordLab.setBounds(64, 77, 70, 15);
		panel.add(passwordLab);
		
		JLabel newPwdLab = new JLabel("确认密码：");
		newPwdLab.setHorizontalAlignment(SwingConstants.RIGHT);
		newPwdLab.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		newPwdLab.setBounds(64, 108, 70, 15);
		panel.add(newPwdLab);
		
		newPwd = new JPasswordField();
		newPwd.setBounds(144, 76, 135, 21);
		panel.add(newPwd);
		
		rePwd = new JPasswordField();
		rePwd.setBounds(144, 105, 135, 21);
		panel.add(rePwd);
		
		tipLab = new JLabel("");
		tipLab.setForeground(Color.RED) ;
		tipLab.setFont(new Font("微软雅黑", Font.PLAIN, 13)) ;
		tipLab.setHorizontalAlignment(SwingConstants.CENTER);
		tipLab.setBounds(92, 200, 201, 14);
		panel.add(tipLab);
		
		JButton ok = new JButton("确认");
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String orignPwd = new String(ogrPAss.getPassword()) ;
				String password = new String(newPwd.getPassword()); 
				String repassword = new String(rePwd.getPassword()) ;
				if("".equals(orignPwd)) {
					tipLab.setText("原始密码不能为空");
					return ;
				}
				
				if("".equals(password) || password==null || "".equals(repassword)||repassword==null){
					tipLab.setText("请输入密码或确认密码") ;
					return ;
				}
				
				ECPacket p = new ECPacket() ;
				p.setMessageType("MODIFY_PASS") ;
				p.setFrom(ClientContext.getLoginUser().getUserCode()) ;
				p.addParam("o_pass", orignPwd) ;
				p.addParam("new_pass", password) ;
				
				try {
					JDialog jd = CommonDialog.createModelWaitDialog(ModifyPwd.this);
					ClientContext.putValue(ClientConst.CURRENT_WAITING_DIALOG, jd);
					ClientContext.putValue(ClientConst.CURRENT_REQUESTOR, ModifyPwd.this) ;
					ClientContext.getContext().getClient().sendMessage(p);
					new java.util.Timer().schedule(new TimerTask() {
						public void run() {
							JDialog dia = ClientContext.getValue(ClientConst.CURRENT_WAITING_DIALOG,JDialog.class) ;
							dia.dispose() ;
							tipLab.setText("修改密码失败") ;
						}
					}, 3*60*1000) ;
				} catch (Exception e1) {
					tipLab.setText("修改密码失败") ;
				}
				
				
			}
		});
		ok.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		ok.setBounds(126, 158, 62, 23);
		panel.add(ok);
		
		JButton cancel = new JButton("取消");
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		cancel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		cancel.setBounds(198, 158, 62, 23);
		panel.add(cancel);
		

	}
}
